using System.Collections.Generic;
using System.Linq;
using BlogExample.Core.Domain;

namespace BlogExample.Core.Services
{
	public interface IDisplayService
	{
		string GetDisplayFor(DomainEntity entity);
	}

	public class DisplayService : IDisplayService
	{
		private readonly List<IDisplayStrategy> _strategies = new List<IDisplayStrategy>();

		public DisplayService(IEnumerable<IDisplayStrategy> strategies)
		{
			_strategies.AddRange(strategies);
			_strategies.Add(new IdentifierDisplayStrategy());
			_strategies.Add(new DefaultDisplayStrategy());
		}

		public string GetDisplayFor(DomainEntity entity)
		{
			var strategy = FindStrategyFor(entity);
			return strategy.GetEntityDescription(entity);
		}

		public IDisplayStrategy FindStrategyFor(DomainEntity entity)
        {
            return _strategies.First(s => s.Matches(entity));
        }
	}
}